"""
    Author:
    Date: 2018-12-29

    照片管理类
"""

import json
import env
import requests
from utils.log import Log


def request_api_server(api_name, params):
    """ 调用API服务器的接口 """
    url = f'{env.VCHAT_API_SERVER_URL}{api_name}'
    r = requests.get(url, params=params)
    if r.status_code != 200:
        return None
    json_data = r.json()
    Log.info(f'Call api {api_name}: ret={json_data}')

    data = json_data['data']
    if data['code'] != 0:
        Log.error(f'Call api {api_name} failed: {json_data}')
        return None
    return data['info']


def update_user_photo(params):
    """ 更新用户照片 """
    return request_api_server('admin.updateUserPhoto', params)


def update_user_profile(params):
    """ 更新用户资料 """
    return request_api_server('admin.updateUserProfile', params)


# def update_user_moment(params):
#     """ 更新用户动态 """
#     return request_api_server('admin.updateUserMoment', params)


def add_user_diamond(uid, amount, gm, comment):
    """ 给用户增加钻石 """
    params = {
        'uid': uid,
        'amount': amount,
        'gm': gm,
        'comment': comment,
    }
    return request_api_server('admin.addUserDiamond', params)


def set_user_is_busy(uid, is_busy):
    """ 设置用户的勿扰模式
        如果是关闭勿扰模式，则结算在线时长
    """
    params = {
        'uid': uid,
        'is_busy': is_busy,
    }
    return request_api_server('admin.setUserIsBusy', params)


# def set_user_allow_free_chat(uid, allowed):
#     """ 设置用户的是否允许免费通话 """
#     params = {
#         'uid': uid,
#         'allowed': allowed,
#     }
#     return request_api_server('admin.setUserAllowFreeChat', params)


def set_user_recommend_value(uid, recommend_value):
    """ 设置用户的推荐度 """
    params = {
        'uid': uid,
        'recommend_value': recommend_value,
    }
    return request_api_server('admin.setUserRecommendValue', params)


def set_cast_price(uid, price):
    """ 设置用户的直播价格 """
    params = {
        'uid': uid,
        'price': price,
    }
    return request_api_server('admin.setUserCastPrice', params)


def user_logout(uid):
    """ 让用户退出登录 """
    params = {
        'uid': uid,
    }
    return request_api_server('admin.userLogout', params)


def refresh_user_cache(uid):
    """ 刷新用户缓存 """
    params = {
        'uid': uid,
    }
    return request_api_server('admin.refreshUserCache', params)


def agree_broadcaster_auth(uid):
    """ 授权对主播的认证 """
    params = {
        'uid': uid,
    }
    return request_api_server('admin.agreeBroadcasterAuth', params)


def reject_broadcaster_auth(uid):
    """ 撤销对主播的认证 """
    params = {
        'uid': uid,
    }
    return request_api_server('admin.rejectBroadcasterAuth', params)


def send_hx_message(uid, message):
    """ 给单个用户发送私信 """
    params = {
        'uid': uid,
        'message': message,
    }
    return request_api_server('admin.sendHxMessage', params)


def send_hx_message_to_users(uids, message):
    """ 给多个用户发送私信 """
    params = {
        'uids': uids,
        'message': message,
    }
    return request_api_server('admin.sendHxMessageToUsers', params)


def shutdown_channel(channel_name):
    """ 关闭指定的频道 """
    params = {
        'channel_name': channel_name,
    }
    return request_api_server('admin.shutdownChannel', params)


def create_wx_pay_order(uid, rmb, mch_id=''):
    """ 创建微信支付订单 """
    params = {
        'uid': uid,
        'rmb': rmb,
        'mch_id': mch_id,
    }
    return request_api_server('admin.createWxPayOrder', params)


def create_api_wap_pay_order(uid, rmb):
    """ 创建支付宝手机网站支付订单 """
    params = {
        'uid': uid,
        'rmb': rmb,
    }
    return request_api_server('admin.createAliWapPayOrder', params)


def set_caster_star(uid, star):
    """ 设置主播的星级 """
    params = {
        'uid': uid,
        'star': star,
    }
    return request_api_server('admin.setCasterStar', params)


def set_video_price(vid, price):
    """ 设置视频价格 """
    params = {
        'vid': vid,
        'price': price,
    }
    return request_api_server('admin.setVideoPrice', params)


def modify_caster_nickname(uid, nickname):
    """ 修改主播的昵称 """
    params = {
        'uid': uid,
        'nickname': nickname,
    }
    return request_api_server('admin.modifyCasterNickname', params)


def modify_caster_signature(uid, signature):
    """ 修改主播的签名 """
    params = {
        'uid': uid,
        'signature': signature,
    }
    return request_api_server('admin.modifyCasterSignature', params)


def modify_caster_introduction(uid, introduction):
    """ 修改主播的介绍 """
    params = {
        'uid': uid,
        'introduction': introduction,
    }
    return request_api_server('admin.modifyCasterIntroduction', params)


def refund_video_chat(liveid):
    """ 修改主播的介绍 """
    params = {
        'liveid': liveid,
    }
    return request_api_server('admin.refundVideoChat', params)


def push_notice_message_to_user(uid, message, extras, apns_production):
    """ 修改主播的介绍 """
    params = {
        'uid': uid,
        'message': message,
        'extras': extras,
        'apns_production': apns_production,
    }
    return request_api_server('admin.pushNoticeMessageToUser', params)


def prepay_with_hsq_mp(uid, amount, appid, openid, client_ip):
    """ 慧收钱公众号预充值 """
    params = {
        'uid': uid,
        'amount': amount,
        'appid': appid,
        'openid': openid,
        'client_ip': client_ip,
    }
    return request_api_server('admin.prepayWithHsqMp', params)


if __name__ == '__main__':
    print('ok')
